package com.lianrui.app.order.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianrui.app.order.domain.Order;
import com.lianrui.app.order.services.IOrderService;
import com.lianrui.app.order.services.OrderStatus;
import com.lianrui.app.order.vos.OrderPage;
import com.lianrui.commons.ResultView;
import com.lianrui.commons.exception.BusinessException;
import com.lianrui.core.AbstractController;
import com.lianrui.core.IService;

/**
 * 功能描述：订单控制器
 *
 * @author LJQ
 *
 * 2015年12月19日 下午3:45:20
 */
@Controller
@RequestMapping("/order")
public class OrderController extends AbstractController<OrderPage, Order, Integer> {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);
	
	@Autowired
	IOrderService orderService;
	
	/**
	 * 功能说明：创建订单-购物车方式
	 * @return
	 */
	@RequestMapping("/create/{cartIds}")
	@ResponseBody
	public ResultView<Order> create(@PathVariable Integer... cartIds) {
		ResultView<Order> view = new ResultView<Order>();
		try {
			view.setResult(orderService.create(null, null, cartIds));
		} catch (BusinessException e) {
			view.setCode(e.getCode());
			view.getMessage();
		}
		return view;
	}
	
	/**
	 * 功能说明：创建订单-直接购买方式
	 * @param model 视图模型
	 * @return
	 */
	@RequestMapping("/create/{productId}/{num}")
	@ResponseBody
	public ResultView<Order> create(@PathVariable Integer productId,@PathVariable Integer num) {
		ResultView<Order> view = new ResultView<Order>();
		try {
			view.setResult(orderService.create(productId, num));
		} catch (BusinessException e) {
			view.setCode(e.getCode());
			view.getMessage();
		}
		return view;
	}
	
	@Override
	public IService<OrderPage, Order, Integer> getService() {
		return orderService;
	}

	@Override
	public Logger getLogger() {
		return LOGGER;
	}
	
	/**
	 * 取消订单
	 * @param id ： 订单id
	 * @return
	 */
	@RequestMapping("/cancel/{id}")
	@ResponseBody
	public ResultView<Void> cancelOrder(@PathVariable Integer id) { 
		ResultView<Void> view = new ResultView<Void>();
		try {
			orderService.updateOrderStatus(id, OrderStatus.ORDER_CANCEL);
		} catch (BusinessException e) {
			view.setCode(e.getCode());
			view.setMessage(e.getMessage());
		}
		return view;
	}
	
	/**
	 * 功能说明：手机端简易支付成功
	 * @param id 订单编码
	 * @return
	 */
	@RequestMapping("/payMobile/{id}")
	public ResultView<Void> payMobile(@PathVariable Integer id) {
		ResultView<Void> view = new ResultView<Void>();
		try {
			orderService.payMobile(id);
		} catch (BusinessException e) {
			view.setCode(e.getCode());
			view.setMessage(e.getMessage());
		}
		return view;
	}
	
}
